package com.xfreelance.qcshop.utils;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.xfreelance.qcshop.configure.DptConfig;
import com.xfreelance.qcshop.exception.DptException;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class DptUtil {

    @Autowired
    private DptConfig dptConfig;

    @Data
    private class DptResponse {
        private Integer OperateCode;
        private String ErrorMessage;
        private Object obj;
    }

    private Object doDptRequest(String api, String paramStr) throws DptException {
        String response = HttpRequest.post(api)
                .body(paramStr)
                .timeout(20000)//超时，毫秒
                .execute().body();
        DptResponse dptResponse = JSONUtil.toBean(response, DptResponse.class);
        String errorMessage = dptResponse.getErrorMessage();
        if (errorMessage == null || errorMessage.length() > 0) {
            System.out.println(String.format("dpt request failed, api:%s, paramStr:%s, msg:%s", api, paramStr, errorMessage));
            throw new DptException(errorMessage);
        }

        return dptResponse.getObj();
    }
//    商品条码查询商品信息
    public String getAccCode(String code) throws DptException {
        String assCode = null;
        Map<String, Object> body = new HashMap<>();
        body.put("nodeCode", dptConfig.getNodeCode());
        body.put("code", code);
        List<Map<String, Object>> obj = (List<Map<String, Object>>)doDptRequest(dptConfig.getDomain() + dptConfig.getApis().get("ByCode"), JSONUtil.toJsonStr(body));
        return obj.get(0).get("AssCode").toString();
    }


//获取商品详细信息
    public Map<String, Object> getDetailInfo(String assCode) throws DptException {
        Map<String, Object> body = new HashMap<>();
        body.put("nodeCode", dptConfig.getNodeCode());
        body.put("goodscode", assCode);

        List<Map<String, Object>> obj = (List<Map<String, Object>>)doDptRequest(dptConfig.getDomain() + dptConfig.getApis().get("DetailInfo"), JSONUtil.toJsonStr(body));

        Map<String, Object> items = new HashMap<>();
        obj.get(0).forEach((k, v) -> {
            if (v instanceof String && !((String) v).trim().isEmpty()) {
                items.put(k, v);
            }
        });

        return items;
    }
//获取商品促销信息
    public Map<String, Object> getPromotion(String assCode) throws DptException {
        Map<String, String> body = new HashMap<>();
        body.put("nodeCode", dptConfig.getNodeCode());
        body.put("goodscode", assCode);
        body.put("queryCode", "02");

        List<Map<String, Object>> obj = (List<Map<String, Object>>)doDptRequest(dptConfig.getDomain() + dptConfig.getApis().get("Promotion"), JSONUtil.toJsonStr(body));

        Map<String, Object> items = new HashMap<>();
        obj.get(0).forEach((k, v) -> {
            if (v instanceof String && !((String) v).trim().isEmpty()) {
                items.put(k, v);
            }
        });

        return items;
    }
//获取商品销售数量

//加载商品货架信息
}
